//
//  MusicViewController.m
//  老司机发车了
//
//  Created by 斌 on 16/8/5.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "MusicViewController.h"


#import "HandPickTableViewController.h"
#import "RankListTableViewController.h"
#import "MusicListViewController.h"
#import "UserViewController.h"

#import "TitleLabel.h"

#import "MusicManager.h"
#import "PlayerModel.h"

#import "PlayerViewController.h"

@interface MusicViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) NSArray *titleArray;

@property (weak, nonatomic) IBOutlet UIScrollView *titleScrollView;

@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;

@property (nonatomic, strong) UIView *baseView;

@property (nonatomic, strong) UILabel *songLabel;

@property (nonatomic, strong) UILabel *singerLabel;

@property (nonatomic, strong) UIImageView *imgView;

@property (nonatomic, strong) UIButton *btn;

@end

@implementation MusicViewController

- (NSArray *)titleArray{
    
    if (!_titleArray) {
        _titleArray = [[NSArray alloc] init];
    }
    return _titleArray;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.titleArray = @[@"精选",@"排行榜",@"歌手",@"我的"];
    
    self.contentScrollView.delegate = self;
    self.contentScrollView.contentSize = CGSizeMake(screenWidth * _titleArray.count, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    [self addTitleScrollView];
    [self addChildViewController];
    [self addlitterController];
    
    [self scrollViewDidEndDecelerating:self.contentScrollView];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:@"isPlay" object:nil];
}

- (void)addlitterController{
    
    _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, screenheight - 108, screenWidth, 64)];
    _baseView.backgroundColor = [UIColor whiteColor];
    
    _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 7, 50, 50)];
    _imgView.layer.cornerRadius = 25;
    _imgView.layer.masksToBounds = YES;
    
    [_baseView addSubview:_imgView];
    
    _songLabel = [[UILabel alloc] initWithFrame:CGRectMake(_imgView.endX + 10, 10, screenWidth - 130, 20)];
    _singerLabel = [[UILabel alloc] initWithFrame:CGRectMake(_songLabel.ownX, _songLabel.endY + 10, _songLabel.ownWidth, 14)];
    _songLabel.font = [UIFont systemFontOfSize:15];
    _singerLabel.font = [UIFont systemFontOfSize:13];
    [_baseView addSubview:_songLabel];
    [_baseView addSubview:_singerLabel];
    
    _btn = [[UIButton alloc] initWithFrame:CGRectMake(screenWidth - 70, 12, 40, 40)];
    [_btn setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
    [_btn addTarget:self action:@selector(playAndPause:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:_btn];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    [_baseView addGestureRecognizer:tap];
    
    [self.view addSubview:_baseView];
    
    _baseView.hidden = YES;
}

- (void)change{
    
    [self.baseView addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
        _baseView.hidden = NO;
    
    if ([MusicManager shareMusicManager].isPlay == YES) {
        [_btn setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
        PlayerModel *playmodel = [MusicManager shareMusicManager].playerModel;
        [_imgView sd_setImageWithURL:[NSURL URLWithString:playmodel.pic_radio] placeholderImage:[UIImage imageNamed:@"wolf"]];
        _singerLabel.text = playmodel.author;
        _songLabel.text = playmodel.title;
    }else{
        
        [_btn setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
    }
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    if ([keyPath isEqualToString:@"hidden"]) {
        
        [self scrollViewDidEndDecelerating:self.contentScrollView];
        [self.baseView removeObserver:self forKeyPath:@"hidden"];
    }
    
}



- (void)tapClick{
    
    PlayerViewController *playVC = [[PlayerViewController alloc] init];
    UINavigationController *playNC = [[UINavigationController alloc] initWithRootViewController:playVC];
    PlayerModel *playmodel = [MusicManager shareMusicManager].musicArray[[MusicManager shareMusicManager].index];
    playVC.songID = playmodel.song_id;
    if (playVC.songID) {
        
        [self.navigationController presentViewController:playNC animated:YES completion:nil];
    }
    
}


- (void)playAndPause:(UIButton *)sender{
    UIButton *btn = sender;
    if ([MusicManager shareMusicManager].isPlay == YES) {
        [btn setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
        [[MusicManager shareMusicManager] stopPlayMusic];
    }else{
        [btn setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
        [MusicManager shareMusicManager].isPlay = YES;
        [[MusicManager shareMusicManager] startPlayMusic];
    }
    
}



- (void)addTitleScrollView{
    
    for (NSInteger i = 0; i < _titleArray.count; i++) {
        
        TitleLabel *titleLabel = [[TitleLabel alloc] init];
        titleLabel.tag = i;
        titleLabel.ownX = i * screenWidth / 4;
        titleLabel.text = self.titleArray[i];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [titleLabel addGestureRecognizer:tap];
        [self.titleScrollView addSubview:titleLabel];
    }
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    NSInteger index = tap.view.tag;
    [self selectLabel:index];
    
    CGPoint currentOffset = self.contentScrollView.contentOffset;
    currentOffset.x = screenWidth * index;

    [self.contentScrollView setContentOffset:currentOffset animated:YES];

}

- (void)selectLabel:(NSInteger)index{
    
    UILabel *currentLabel = self.titleScrollView.subviews[index];
    currentLabel.textColor = [UIColor blueColor];
    for (TitleLabel *tempLabel in self.titleScrollView.subviews) {
        
        if (tempLabel != currentLabel) {
            tempLabel.textColor = [UIColor blackColor];
        }
        
    }
}

- (void)addContentScrollView:(NSInteger)index{
    
    UIViewController *VC = self.childViewControllers[index];
    
    if ([VC isViewLoaded]) {
        return;
    }
    if (self.baseView.hidden == YES) {
        
        VC.view.frame = CGRectMake(index * screenWidth, 0, screenWidth, self.contentScrollView.ownHeight);
    }else{
        VC.view.frame =  CGRectMake(index * screenWidth, 0, screenWidth, self.contentScrollView.ownHeight - 64);
    }
    [self.contentScrollView addSubview:VC.view];
}

- (void)addChildViewController{
    
    HandPickTableViewController *handPickVC = [[HandPickTableViewController alloc] init];
    
    [self addChildViewController:handPickVC];
    
    RankListTableViewController *rankListVC = [[RankListTableViewController alloc] init];
    [self addChildViewController:rankListVC];
    
    MusicListViewController *musicVC = [[MusicListViewController alloc] init];
    [self addChildViewController:musicVC];
    
    UserViewController *userVC = [[UserViewController alloc] init];
    
    [self addChildViewController:userVC];

}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    
    CGPoint offset = self.contentScrollView.contentOffset;
    NSInteger index = offset.x / screenWidth;
    
    [self addContentScrollView:index];
    [self selectLabel:index];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    [self scrollViewDidEndScrollingAnimation:scrollView];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
